from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from try_mysql import *
from PyQt5.uic import loadUiType
from movie_record_manage_ui import Ui_Movie_record
import sys
ui, _ = loadUiType('movie_record_manage.ui')

class Movie_record_manage_App(QMainWindow, ui):
    #定义构造
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        #self.handle_ui_change()
        self.handle_buttons()
        self.show()
        self.show_tab()

    def handle_buttons(self):
        self.comfirm_add_rrecord.clicked.connect(self.Confirm_add)
        self.drop_record.clicked.connect(self.Drop_record)
        self.update_record.clicked.connect(self.Update_record)
        self.query_record.clicked.connect(self.Query_record)

    #1.添加类别
    def Confirm_add(self):
        conn = get_conn()
        cur = conn.cursor()
        MovieName = self.add_movie_name.text()
        TimeRecord = self.add_time_record.text()
        SessionRcord = self.add_sessions_record.text()
        MovieGrade = self.add_movie_grade.text()
        FilmHouse = self.film_house.text()
        Population = self.population_movie.text()
        if (MovieName== "" or TimeRecord == "" or SessionRcord == "" or MovieGrade == "" or FilmHouse == "" or Population == ""):
            self.statusBar().showMessage('字段为空添加有毛用！')
            return
        else:
            sql = "insert into movie_record_manage(MovieName,TimeRecord,SessionRcord,MovieGrade,FilmHouse,Population) VALUES (%s,%s,%s,%s,%s,%s)"
            values=(MovieName,TimeRecord,SessionRcord,MovieGrade,FilmHouse,Population)
            cur.execute(sql, values)
            conn.commit()
            close_conn(conn, cur)
            self.statusBar().showMessage('好耶！添加成功!')
            self.show_tab()
            self.clearEdit()
        return



    def show_tab(self):
        conn=get_conn()
        cur=conn.cursor()
        sql="select * from movie_record_manage order by MovieGrade asc"
        cur.execute(sql)
        data = cur.fetchall()
        if data:
            self.view_record_tab.setRowCount(0)
            self.view_record_tab.insertRow(0)
            for row , form in enumerate(data):
                for column ,item in enumerate(form):
                    self.view_record_tab.setItem(row,column,QTableWidgetItem(str(item)))
                    column += 1
                row_position=self.view_record_tab.rowCount()
                self.view_record_tab.insertRow(row_position)



    def Drop_record(self):
        conn = get_conn()
        cur = conn.cursor()
        MovieName = self.add_movie_name.text()
        TimeRecord = self.add_time_record.text()
        SessionRcord = self.add_sessions_record.text()
        MovieGrade = self.add_movie_grade.text()
        FilmHouse = self.film_house.text()
        Population = self.population_movie.text()
        if (MovieName== "" or SessionRcord == "" or FilmHouse == ""):
            self.statusBar().showMessage('要删哪条好歹多给点提示')
            return
        else:
            sql1 = "delete from movie_record_manage where MovieName=%s and TimeRecord=%s and SessionRcord=%s and MovieGrade=%s and FilmHouse=%s and Population=%s"
            cur.execute(sql1, (MovieName,TimeRecord,SessionRcord,MovieGrade,FilmHouse, Population))
            conn.commit()
            close_conn(conn, cur)
            self.statusBar().showMessage('你真棒还把爷删了。。。')
            self.show_tab()
            self.clearEdit()
        return


    def Update_record(self):
        conn = get_conn()
        cur = conn.cursor()
        MovieName = self.add_movie_name.text()
        TimeRecord = self.add_time_record.text()
        SessionRcord = self.add_sessions_record.text()
        MovieGrade = self.add_movie_grade.text()
        FilmHouse = self.film_house.text()
        Population = self.population_movie.text()
        if (MovieName == ""):
            self.statusBar().showMessage('你到底要改条具体一点ok?')
            return
        else:
            sql1 = "update movie_record_manage set MovieName=%s,TimeRecord=%s, SessionRcord=%s ,MovieGrade=%s ,FilmHouse=%s ,Population=%s where MovieName=%s"
            values = (MovieName, TimeRecord, SessionRcord, MovieGrade, FilmHouse, Population,MovieName)
            cur.execute(sql1, values)
            conn.commit()
            close_conn(conn, cur)
            self.statusBar().showMessage('爷，我更新好了')
            self.show_tab()
            self.clearEdit()

    def Query_record(self):
        conn = get_conn()
        cur = conn.cursor()
        MovieName = self.add_movie_name.text()
        TimeRecord = self.add_time_record.text()
        SessionRcord = self.add_sessions_record.text()
        MovieGrade = self.add_movie_grade.text()
        FilmHouse = self.film_house.text()
        Population = self.population_movie.text()
        sql2 = "select * from movie_record_manage where MovieName=%s or TimeRecord=%s or SessionRcord=%s or MovieGrade=%s or FilmHouse=%s or Population=%s"
        cur.execute(sql2, (MovieName, TimeRecord, SessionRcord, MovieGrade, FilmHouse, Population))
        data = cur.fetchall()
        if data:
            self.view_record_tab.setRowCount(0)
            self.view_record_tab.insertRow(0)
            for row, form in enumerate(data):
                for column, item in enumerate(form):
                    self.view_record_tab.setItem(row, column, QTableWidgetItem(str(item)))
                    column += 1
                row_position = self.view_record_tab.rowCount()
                self.view_record_tab.insertRow(row_position)
                self.statusBar().showMessage('老哥你要的结果。')
        else:
            self.statusBar().showMessage('搜不到搜不到！！！')


    def clearEdit(self):
        self.add_movie_name.clear()
        self.add_time_record.clear()
        self.add_sessions_record.clear()
        self.add_movie_grade.clear()
        self.film_house.clear()
        self.population_movie.clear()

def main():
    app = QApplication([])
    window = Movie_record_manage_App()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()